﻿#include "VAOneXMLAdaptorPhysicalPropertyRibInfo.h"

#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyRibbed.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyRibInfo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

namespace IO
{

    bool VAOneXMLAdaptorPhysicalPropertyRibInfo::adaptR()
    {
        return readRibInfo();
    }

    bool VAOneXMLAdaptorPhysicalPropertyRibInfo::adaptW()
    {

        return writeRibbed();
    }

    void VAOneXMLAdaptorPhysicalPropertyRibInfo::setAxisNumber(int number)
    {
        _axisNumber = number;
    }

    bool VAOneXMLAdaptorPhysicalPropertyRibInfo::readRibInfo()
    {
        if(!_node)//判断节点是否为空
        {
            return false;
        }

        //转换类型为加筋信息
        Acoustics::FITKPhyscisPropertyRibInfo* ribInfo = dynamic_cast<Acoustics::FITKPhyscisPropertyRibInfo*>(_dataObj);

        if(!ribInfo)//类型转换是否成功
        {
            return false;
        }

        bool b = false;

        //获取属性
        QString nameValue = readStrAttribute(_node, "BeamPropRef", &b);

        if(b)
        {
            //根据名称查找id
            int id = getPhysicalPropertyObjectIdByName(nameValue);
            ribInfo->setBeamPropID(id);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read Base Board Error" + nameValue);
        }

        //设置类名称
        ribInfo->setDataObjectName(nameValue);
        double offsetValue = readDoubleAttribute(_node, "offset", &b);

        if(b)
        {
            ribInfo->setOffset(offsetValue);
        }

        //获取加筋方向
        QString orientationValue = readStrAttribute(_node, "orientation", &b);

        //判断 加筋方向，后续可替换成枚举类型
        if(orientationValue == "LEFT")
        {
            ribInfo->setOrientation(0);
        }
        else if(orientationValue == "UP")
        {
            ribInfo->setOrientation(90);
        }
        else if(orientationValue == "RIGHT")
        {
            ribInfo->setOrientation(180);
        }
        else if(orientationValue == "DOWN")
        {
            ribInfo->setOrientation(270);
        }

        double spacingValue = readDoubleAttribute(_node, "spacing", &b);

        if(b)
        {
            ribInfo->setSpacing(spacingValue);
        }

        return true;
    }

    bool VAOneXMLAdaptorPhysicalPropertyRibInfo::writeRibbed()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKPhyscisPropertyRibInfo* data = dynamic_cast<Acoustics::FITKPhyscisPropertyRibInfo*>(_dataObj);

        if(!data)
        {
            return false;
        }

        if(!_node)
        {
            return false;
        }

        //写如属性值
        //写厚度
        writeAttribute(_node, "axis_number", _axisNumber);
        writeAttribute(_node, "spacing", data->getSpacing());

/*
        if (qFuzzyCompare(angle, 0.0)) return 0;
        if (qFuzzyCompare(angle, 90.0)) return 1;
        if (qFuzzyCompare(angle, 180.0)) return 2;
        if (qFuzzyCompare(angle, 270.0)) return 3;

    */

        if(qFuzzyCompare(data->getOrientation(), 0))
        {
            writeAttribute(_node, "orientation", "LEFT");
        }
        else if(qFuzzyCompare(data->getOrientation(), 90))
        {
            writeAttribute(_node, "orientation", "UP");
        }//
        else if(qFuzzyCompare(data->getOrientation(), 180))
        {
            writeAttribute(_node, "orientation", "RIGHT");
        }
        else if(qFuzzyCompare(data->getOrientation(), 270))
        {
            writeAttribute(_node, "orientation", "DOWN");
        }

        writeAttribute(_node, "offset", data->getOffset());
        writeAttribute(_node, "BeamPropRef", getPhysicalPropertyObjectNameById(data->getBeamPropID()));
        return true;
    }

    int IO::VAOneXMLAdaptorPhysicalPropertyRibInfo::getPhysicalPropertyObjectIdByName(QString name)
    {
        //获取全局数据管理器
        auto globalData = FITKAPP->getGlobalData();

        if (!globalData)
        {
            return -1;// 如果转换失败，材料名称无法填写
        }

        //获取管理器
        Acoustics::FITKAcousticsPhysics* physics = globalData->getPhysicsData<Acoustics::FITKAcousticsPhysics>();

        if (!physics)
        {
            return -1;  // 如果转换失败，材料名称无法填写
        }

        //获取材料管理器
        auto manager = physics->getPhysicsPropManager();

        if (!manager)
        {
            return -1;
        }

        //根据名称获取材料类指针
        Acoustics::FITKAcousticsAbsPhysicsProp* data = manager->getDataByName(name);

        if (!data)
        {
            return -1;
        }
        return data->getDataObjectID();
        // 没有找到匹配项，返回 -1
    }

    QString VAOneXMLAdaptorPhysicalPropertyRibInfo::getPhysicalPropertyObjectNameById(int id)
    {
        //获取全局数据管理器
        auto globalData = FITKAPP->getGlobalData();

        if (!globalData)
        {
            return "";// 如果转换失败，名称无法填写
        }

        //获取管理器
        Acoustics::FITKAcousticsPhysics* physics = globalData->getPhysicsData<Acoustics::FITKAcousticsPhysics>();

        if (!physics)
        {
            return "";  // 如果转换失败，名称无法填写
        }

        //获取管理器
        auto manager = physics->getPhysicsPropManager();

        if (!manager)
        {
            return "";
        }

        //根据名称获取类指针
        Acoustics::FITKAcousticsAbsPhysicsProp* data = manager->getDataByID(id);

        if (!data)
        {
            return "";
        }
        return data->getDataObjectName();
    }
}
